package com.example.yyy

import android.content.DialogInterface
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.ListView
import kotlinx.android.synthetic.main.activity_view.*
import org.jetbrains.anko.selector
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.toast


class ViewActivity : AppCompatActivity() {

    private val strArray = arrayOf("水星","金星","木星","火星","地球","土星" )

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_view)
        initSpinner()
        initSpinnerKt()
        initListView()
    }


    private fun initListView() {
        var mutableList :MutableList<Planet> = mutableListOf()
        var planet = Planet(R.drawable.ic_launcher_background, "水星")
        mutableList.add(planet)
        mutableList.add(planet)
        mutableList.add(planet)
        mutableList.add(planet)
        lv_vw_listView.adapter = MyListAdapter(this,mutableList)
        gv_vw_gridView.adapter = MyListAdapter(this,mutableList)
    }

    private fun initSpinnerKt() {
        val sateArray = listOf("水星","金星","木星","火星","地球","土星" )
        tv_vw_spinner.text = sateArray[0]
        tv_vw_spinner.setOnClickListener {
            selector("请选择行星",sateArray){ _: DialogInterface, i: Int ->
                tv_vw_spinner.text = sateArray[i]
                toast("你选择了${strArray[i]} ")
            }
        }
    }

    private fun initSpinner() {
        var strAdapter = ArrayAdapter(this, R.layout.support_simple_spinner_dropdown_item, strArray)
        strAdapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item)
        sp_vw_spinner.prompt = "请选择行星"
        sp_vw_spinner.adapter = strAdapter
        sp_vw_spinner.setSelection(0)
        sp_vw_spinner.onItemSelectedListener = MySelectedListener()
    }
    internal inner class MySelectedListener : AdapterView.OnItemSelectedListener{
        override fun onNothingSelected(parent: AdapterView<*>?) {
            TODO("Not yet implemented")
        }

        override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
            toast("你选择了${strArray[position]} ")
        }

    }
}